package com.gao.id.factory;

import com.gao.common.Constants;
import com.gao.id.IdGenerator;
import com.gao.id.strategy.RedisId;
import com.gao.id.strategy.SegmentId;
import com.gao.id.strategy.SnowFlakeId;
import com.gao.id.strategy.UUId;
import com.gao.id.support.segment.Segment;
import io.netty.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略工厂
 */
@Service
public class IDStrategyFactory {

    private static Map<String, IdGenerator> idGeneratorMap = new ConcurrentHashMap<>();

    /**
     * 提供一个方法，从工厂中获取策略
     * @param idCode
     * @return
     */
    public IdGenerator getIDGenerator(String idCode) {
        return idGeneratorMap.get(idCode);
    }

    @Autowired
    private UUId uuid;

    @Autowired
    private RedisId redisId;

    @Autowired
    private SnowFlakeId snowFlakeId;

    @Autowired
    private SegmentId segmentId;

    /**
     * 利用工厂对策略进行初始化
     */
    @PostConstruct
    public void init() {
        idGeneratorMap.put(Constants.Ids.UUID.getCode(), uuid);
        idGeneratorMap.put(Constants.Ids.RedisId.getCode(), redisId);
        idGeneratorMap.put(Constants.Ids.SnowFlakeId.getCode(), snowFlakeId);
        idGeneratorMap.put(Constants.Ids.SegmentId.getCode(), segmentId);
    }
}
